function queue_dynamics_delayed_approx(provisioning_time,hold_duration)
disp('delayed approx'); 
randn('seed',12345)
lambda = randn(1,400);
%lambda(100:200)=4+lambda(100:200);
%lambda=lambda+10;
% randn('seed',16576576576);  k=cumsum(randn(200,1)*5 ); k= k-min(k); plot(k)
lambda = lambda + sin((1:400)/20)*10+30;
lambda = [0 lambda];
lambda(181:end)=0;
a = 1;
b = [0.5/4 3.5/4];
lambda_smooth = filter(b,a,lambda);

cost_U=[]; cost_X=[];
gama_size=9;
r_=logspace( -8, 4, gama_size ); 
for i=1:size(r_,2)
    r=r_(i);
    
target_q=10;
x0=20; 
% prices_amz().get_hourly_price()./prices_amz().get_compute_units()
inst0 = [0;0;0];

%provisioning_time=5; %5
%hold_duration=1;   %60
% A=[0 0 0
%       1 0 0
%       0 1 0];
stations=provisioning_time+hold_duration;
A=[zeros(1,stations-1)
      eye(stations-1)];
A=[A zeros(stations,1)];   
Ain=zeros(stations,1); 
Ain(1,1)=1; 

 T=180;
% %m=mean(lambda(:,2:T+1));
 Vartheta0=zeros(stations,1); 
 Vartheta0(provisioning_time+1,1) = 5; 
 D=[zeros(1,provisioning_time) ones(1,hold_duration)];
cvx_begin quiet  
    variables X(1,T+1) Vartheta(stations,T+1) U(1,T)  cost_r
    Vartheta(:,2:T+1) == A*Vartheta(:,1:T)+Ain*U(:,1:T);
    X(:,2:T+1) == X(:,1:T)+lambda_smooth(:,2:T+1)-D*Vartheta(:,1:T);  
    X(:,1) == x0;      
    Vartheta(:,1)== Vartheta0;  % ones(stations,1); %[10; 4; 4]; 
    Vartheta>0; 
     X(:,2:T+1)>0;
    cost_r==r*sum(U(:,1:T));
    minimize (sum_square(target_q*ones(1,T)-X(:,2:T+1))...
                        +cost_r)
cvx_end
Xopt=X;
Uopt=U; 

% cvx_begin 
%     variables Xact(1,T+1)
%     Xact(:,2:T+1) == Xact(:,1:T)+lambda(:,2:T+1)-D*Vartheta(:,1:T);  
% cvx_end 
Xact=[];
Xact(1)=x0;
for j=1:T+1
      Xact(j+1)=Xact(j)+lambda(:,j+1)-D*Vartheta(:,j);  
end

cost_X = [cost_X sum(sum_square(Xact(:,2:T+1)-target_q))];
cost_U =[cost_U  sum(Uopt)];

% subplot(3,1,1); stairs(Uopt); title('Uopt'); 
% subplot(3,1,2); plot(Xopt); title('Q opt');
% subplot(3,1,3); plot(Xact); title('Q opt');
% hold on
% %subplot(3,1,3); plot(lambda_smooth(1:T)); title('lambda'); 
% 
% % figure; plot(([zeros(1,provisioning_time) ones(1,hold_duration)]*Vartheta)');
% % subplot(4,1,1); plot(Xopt); title('Xopt') 
% % subplot(4,1,2); plot(Uopt'); title('Uopt') 
% % subplot(4,1,3); plot(Xall); title('Xall')
% % subplot(4,1,4); plot(Uall'); title('Uall') 
% 
% %subplot(3,1,3); plot(lambda(2:end))
 fprintf('.');  
end
[cost_U' cost_X'] 
%h=figure;
style  = {'b-.','g--','r-'}; 
plot( cost_U, cost_X, style{hold_duration/5}, 'LineWidth',2  ); 
xlabel('$J_1$','Interpreter','LaTex')
% xlabel( 'norm(x,1)' );
ylabel( '$J_2$','Interpreter','LaTex' );
% [cost_U(2) cost_X(2)]

%text(cost_U(2), cost_X(2), sprintf('\leftarrow \gamma=%d',r_(2)),  'HorizontalAlignment','left')
grid
if (hold_duration==5)
    for j=[1,round(gama_size/2),gama_size] %90:3:100
        text( cost_U(j), cost_X(j), sprintf('\\leftarrow \\gamma=%d',r_(j)),  'HorizontalAlignment','left')
    end
end
%UtilityLib.print_figure(h,9,7,'figure/tradeoff');
hold on
kk=0; 

% %clear
% % MPC
% for T=1:60 %
% %T = 60; % horizon
% nsteps = 180; % number of steps 
% Vartheta0=zeros(stations,1); 
% Vartheta0(provisioning_time+1,1) = 5;  
% n=1; m=1; 
% x = x0;  vartheta=Vartheta0;
% 
% Xall = zeros(n,nsteps); Uall = zeros(m,nsteps); VarthetaAll=zeros(1,nsteps); 
% alpha_est= 0.75;
% v_cost=10; 
% lambda_hat=0.1576;
% D=[zeros(1,provisioning_time) ones(1,hold_duration)];
% dur = [];
% for i = 1:nsteps
%     fprintf('.');  
%     tic
%     cvx_begin quiet 
%         variables X(1,T+1) U(1,T)  Vartheta(stations,T+1)  cost_r  lmbda(1,T)
%         % here I substituted lambda(:,2:T+1) with [mean(lambda(100:200)) mean(lambda(100:200))]
%         X(:,1) == x;           
%        lmbda == lambda_hat*ones(1,T) %lambda(:,1+i:T+i); %lambda_hat*ones(1,T)
%         X(:,2:T+1) == X(:,1:T)+ lmbda -D*Vartheta(:,1:T);        
%          Vartheta(:,1)== vartheta;  % ones(stations,1); %[10; 4; 4]; 
%          Vartheta(:,2:T+1) == A*Vartheta(:,1:T)+Ain*U(:,1:T);
%          Vartheta>0; 
%          %X(:,2:T+1)>0;
%          cost_r==r*sum(U(:,1:T));
%         minimize (sum_square(target_q*ones(1,T)-X(:,2:T+1))...
%                          +cost_r...
%                          +v_cost *  sum_square(X(:,T+1)-target_q))
%     cvx_end
%     dur(i) = toc;
%     Xall(:,i) = x; u = U(:,1); Uall(:,i) = u;
%     x = x+ lambda(:,i) -D*vartheta;   
%     vartheta = A*vartheta+Ain*u;
%     VarthetaAll(:,i) = D*vartheta; 
%     lambda_hat= alpha_est*lambda_hat+(1-alpha_est)*lambda(:,i+1);
% end
% 
% mpccost = sum(sum_square(Xall-target_q))...
%                         +r*sum(Uall);
% % disp(sprintf('T=%d',T)); disp(dur); 
% disp(sprintf('mpccost=%d',mpccost));               
% disp(sprintf('cvx_slvitr=%d',cvx_slvitr));
% end
% %  plot(Uall)
% % figure
% % plot(Xall);
% 
% kk=0
% 
% exit
% 
% 
% 


